<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Admin_Model extends CI_Model {
	
	public function pageview($limit, $offset) {
		
		$this->db->order_by('id', 'desc');
		$this->db->limit($limit, $offset);
		$query = $this->db->get('pageview');
  		
		$rows = $query->result_array();
		
		return $rows;
	}
	
	public function pageview_total_rows() {
		
		$total_rows = $this->db->count_all('pageview');
		
		return $total_rows;
	}
	
	public function cover($limit, $offset) {
	
		$this->db->order_by('sort', 'asc');
		$this->db->limit($limit, $offset);
		$query = $this->db->get('cover');
	
		$rows = $query->result_array();
	
		return $rows;
	}
	
	public function cover_total_rows() {
	
		$total_rows = $this->db->count_all('cover');
	
		return $total_rows;
	}
	
	public function cover_write_action($title, $date, $font_type, $font_color, $thumb_view, $type, $link, $image) {
		
		$image = json_encode($image);
		
		$this->db->select_min('sort');
		$query = $this->db->get('cover');
		$row = $query->row_array();
		
		$sort = $row['sort'] - 1;
		
		$fields = array(
			'title' => $title,
			'date' => $date,
			'font_type' => $font_type,
			'font_color' => $font_color,
			'thumb_view' => $thumb_view,
			'type' => $type,
			'link' => $link,
			'image' => $image,
			'sort' => $sort,
			'register_date' => date('Y-m-d H:i:s')
		);
		
		$this->db->set($fields);
		
		$this->db->insert('cover');
	}
	
	public function cover_sort_action($id_sort_array) {
		
		foreach($id_sort_array as $row) {
			
			$this->db->set('sort', $row['sort']);
			$this->db->where('id', $row['id']);
			
			$this->db->update('cover');
		}
	}
	
	public function cover_delete_action($id) {
		
		$this->db->where('id', $id);
		
		$this->db->delete('cover');
	}
	
	public function cover_view($id) {
		
		$this->db->where('id', $id);
		
		$query = $this->db->get('cover');
		
		$row = $query->row_array();
		
		return $row;
	}
	
	public function cover_edit_action($id, $title, $date, $font_type, $font_color, $thumb_view, $type, $link, $image) {
	
		$image = json_encode($image);
		
		$fields = array(
				'title' => $title,
				'date' => $date,
				'font_type' => $font_type,
				'font_color' => $font_color,
				'thumb_view' => $thumb_view,
				'type' => $type,
				'link' => $link,
				'image' => $image
		);
		$this->db->set($fields);
		
		$this->db->where('id', $id);
		
		$this->db->update('cover');
	}
	
	public function main() {
	
		$this->db->order_by('sort', 'asc');
		$query = $this->db->get('main');
	
		$rows = $query->result_array();
	
		return $rows;
	}
	
	public function main_sort_action($id_sort_array) {
	
		foreach($id_sort_array as $row) {
	
			$this->db->set('sort', $row['sort']);
			$this->db->where('id', $row['id']);
	
			$this->db->update('main');
		}
	}
	
	public function main_edit_action($id, $view_kind, $view_id) {
		
		$fields = array(
				'view_kind' => $view_kind,
				'view_id' => $view_id
		);
		$this->db->set($fields);
		
		$this->db->where('id', $id);
		
		$this->db->update('main');
	}
	
	public function category($limit, $offset) {
	
		$this->db->order_by('sort', 'asc');
		$this->db->limit($limit, $offset);
		$query = $this->db->get('category');
	
		$rows = $query->result_array();
	
		return $rows;
	}
	
	public function category_rows() {
	
		$this->db->order_by('sort', 'asc');
		$query = $this->db->get('category');
	
		$rows = $query->result_array();
	
		return $rows;
	}
	
	public function category_total_rows() {
	
		$total_rows = $this->db->count_all('category');
	
		return $total_rows;
	}
	
	public function category_write_action($title) {
	
		$this->db->select_min('sort');
		$query = $this->db->get('category');
		$row = $query->row_array();
	
		$sort = $row['sort'] - 1;
	
		$fields = array(
				'title' => $title,
				'sort' => $sort,
				'register_date' => date('Y-m-d H:i:s')
		);
	
		$this->db->set($fields);
	
		$this->db->insert('category');
	}
	
	public function category_sort_action($id_sort_array) {
	
		foreach($id_sort_array as $row) {
				
			$this->db->set('sort', $row['sort']);
			$this->db->where('id', $row['id']);
				
			$this->db->update('category');
		}
	}
	
	public function category_delete_action($id) {
	
		$this->db->where('id', $id);
	
		$this->db->delete('category');
	}
	
	public function category_view($id) {
	
		$this->db->where('id', $id);
	
		$query = $this->db->get('category');
	
		$row = $query->row_array();
	
		return $row;
	}
	
	public function category_edit_action($id, $title) {
	
		$image = json_encode($image);
	
		$fields = array(
				'title' => $title,
		);
		$this->db->set($fields);
	
		$this->db->where('id', $id);
	
		$this->db->update('category');
	}
	
	public function work($limit, $offset) {
	
		$this->db->order_by('sort', 'asc');
		$this->db->limit($limit, $offset);
		$query = $this->db->get('work');
	
		$rows = $query->result_array();
	
		return $rows;
	}
	
	public function work_total_rows() {
	
		$total_rows = $this->db->count_all('work');
	
		return $total_rows;
	}
	
	public function work_write_action($new_view, $security, $password, $title1, $title2, $client, $category_id,  $image, $date, $tag, $content, $relation_id_1, $relation_id_2, $relation_id_3, $relation_id_4) {
		
		$category_id = json_encode($category_id);
		$image = json_encode($image);
		
		$this->db->select_min('sort');
		$query = $this->db->get('work');
		$row = $query->row_array();
		
		$sort = $row['sort'] - 1;
		
		$fields = array(
				'new_view' => $new_view,
				'security' => $security,
				'password' => $password,
				'title1' => $title1,
				'title2' => $title2,
				'client' => $client,
				'category_id' => $category_id,
				'image' => $image,
				'date' => $date,
				'tag' => $tag,
				'content' => $content,
				'relation_id_1' => $relation_id_1,
				'relation_id_2' => $relation_id_2,
				'relation_id_3' => $relation_id_3,
				'relation_id_4' => $relation_id_4,
				'sort' => $sort,
				'register_date' => date('Y-m-d H:i:s')
		);
		
		$this->db->set($fields);
		
		$this->db->insert('work');
	}
	
	public function work_sort_action($id_sort_array) {
	
		foreach($id_sort_array as $row) {
				
			$this->db->set('sort', $row['sort']);
			$this->db->where('id', $row['id']);
				
			$this->db->update('work');
		}
	}
	
	public function work_delete_action($id) {
	
		$this->db->where('id', $id);
	
		$this->db->delete('work');
	}
	
	public function work_view($id) {
	
		$this->db->where('id', $id);
	
		$query = $this->db->get('work');
	
		$row = $query->row_array();
	
		return $row;
	}
	
	public function work_edit_action($id, $new_view, $security, $password, $title1, $title2, $client, $category_id,  $image, $date, $tag, $content, $relation_id_1, $relation_id_2, $relation_id_3, $relation_id_4) {
	
		$category_id = json_encode($category_id);
		$image = json_encode($image);
	
		$fields = array(
				'new_view' => $new_view,
				'security' => $security,
				'password' => $password,
				'title1' => $title1,
				'title2' => $title2,
				'client' => $client,
				'category_id' => $category_id,
				'image' => $image,
				'date' => $date,
				'tag' => $tag,
				'content' => $content,
				'relation_id_1' => $relation_id_1,
				'relation_id_2' => $relation_id_2,
				'relation_id_3' => $relation_id_3,
				'relation_id_4' => $relation_id_4
		);
	
		$this->db->set($fields);
	
		$this->db->where('id', $id);
		
		$this->db->update('work');
	}
	
	public function view_count($mode) {
	
		$this->db->where('id', 1);
	
		$query = $this->db->get('view_count');
		$row = $query->row_array();
		
		return $row[$mode]; 
	}
	
	public function view_count_action($mode, $count) {
		
		$fields = array(
				$mode => $count
		);
		
		$this->db->set($fields);
		
		$this->db->update('view_count');
		
	}
	
	public function blog_category($limit, $offset) {
	
		$this->db->order_by('sort', 'asc');
		$this->db->limit($limit, $offset);
		$query = $this->db->get('blog_category');
	
		$rows = $query->result_array();
	
		return $rows;
	}
	
	public function blog_category_rows() {
	
		$this->db->order_by('sort', 'asc');
		$query = $this->db->get('blog_category');
	
		$rows = $query->result_array();
	
		return $rows;
	}
	
	public function blog_category_total_rows() {
	
		$total_rows = $this->db->count_all('blog_category');
	
		return $total_rows;
	}
	
	public function blog_category_write_action($title) {
	
		$this->db->select_min('sort');
		$query = $this->db->get('blog_category');
		$row = $query->row_array();
	
		$sort = $row['sort'] - 1;
	
		$fields = array(
				'title' => $title,
				'sort' => $sort,
				'register_date' => date('Y-m-d H:i:s')
		);
	
		$this->db->set($fields);
	
		$this->db->insert('blog_category');
	}
	
	public function blog_category_sort_action($id_sort_array) {
	
		foreach($id_sort_array as $row) {
	
			$this->db->set('sort', $row['sort']);
			$this->db->where('id', $row['id']);
	
			$this->db->update('blog_category');
		}
	}
	
	public function blog_category_delete_action($id) {
	
		$this->db->where('id', $id);
	
		$this->db->delete('blog_category');
	}
	
	public function blog_category_view($id) {
	
		$this->db->where('id', $id);
	
		$query = $this->db->get('blog_category');
	
		$row = $query->row_array();
	
		return $row;
	}
	
	public function blog_category_edit_action($id, $title) {
	
		$image = json_encode($image);
	
		$fields = array(
				'title' => $title,
		);
		$this->db->set($fields);
	
		$this->db->where('id', $id);
	
		$this->db->update('blog_category');
	}
	
	public function blog($limit, $offset) {
	
		$this->db->order_by('sort', 'asc');
		$this->db->limit($limit, $offset);
		$query = $this->db->get('blog');
	
		$rows = $query->result_array();
	
		return $rows;
	}
	
	public function blog_total_rows() {
		
		$total_rows = $this->db->count_all('blog');
		
		return $total_rows;
	}
	
	public function blog_write_action($title, $date, $client, $category_id,  $image, $content) {
	
		$category_id = json_encode($category_id);
		$image = json_encode($image);
	
		$this->db->select_min('sort');
		$query = $this->db->get('blog');
		$row = $query->row_array();
	
		$sort = $row['sort'] - 1;
	
		$fields = array(
				'title' => $title,
				'date' => $date,
				'client' => $client,
				'category_id' => $category_id,
				'image' => $image,
				'content' => $content,
				'sort' => $sort,
				'register_date' => date('Y-m-d H:i:s')
		);
	
		$this->db->set($fields);
	
		$this->db->insert('blog');
	}
	
	public function blog_sort_action($id_sort_array) {
	
		foreach($id_sort_array as $row) {
	
			$this->db->set('sort', $row['sort']);
			$this->db->where('id', $row['id']);
	
			$this->db->update('blog');
		}
	}
	
	public function blog_delete_action($id) {
	
		$this->db->where('id', $id);
	
		$this->db->delete('blog');
	}
	
	public function blog_view($id) {
	
		$this->db->where('id', $id);
	
		$query = $this->db->get('blog');
	
		$row = $query->row_array();
	
		return $row;
	}
	
	public function blog_edit_action($id, $title, $date, $client, $category_id, $image, $content) {
		
		$category_id = json_encode($category_id);
		$image = json_encode($image);
	
		$fields = array(
				'title' => $title,
				'date' => $date,
				'client' => $client,
				'category_id' => $category_id,
				'image' => $image,
				'content' => $content
		);
		
		$this->db->set($fields);
	
		$this->db->where('id', $id);
	
		$this->db->update('blog');
	}
	
	public function careers() {
	
		$this->db->order_by('id', 'desc');
		$query = $this->db->get('careers');
	
		$rows = $query->result_array();
	
		return $rows;
	}
	
	public function careers_write_action($title, $height, $content) {
	
		$total_rows = $this->db->count_all('careers');
		if(empty($total_rows)) $view = 'show';
		else $view = 'hide';
			
		$fields = array(
				'title' => $title,
				'height' => $height,
				'content' => $content,
				'view' => $view,
				'register_date' => date('Y-m-d H:i:s')
		);
	
		$this->db->set($fields);
	
		$this->db->insert('careers');
	}
	
	public function careers_view_action($id) {
	
		$fields = array(
				'view' => 'hide'
		);
		$this->db->set($fields);
		$this->db->update('careers');
		
		$this->db->where('id', $id);
		$fields = array(
				'view' => 'show'
		);
		$this->db->set($fields);
		$this->db->update('careers');
	}
	
	public function careers_delete_action($id) {
	
		$this->db->where('id', $id);
	
		$this->db->delete('careers');
	}
	
	public function careers_view($id) {
	
		$this->db->where('id', $id);
	
		$query = $this->db->get('careers');
	
		$row = $query->row_array();
	
		return $row;
	}
	
	public function careers_edit_action($id, $title, $height, $content) {
	
		$fields = array(
				'title' => $title,
				'height' => $height,
				'content' => $content
		);
	
		$this->db->set($fields);
	
		$this->db->where('id', $id);
	
		$this->db->update('careers');
	}
	
	public function project_request($limit, $offset) {
	
		$this->db->order_by('id', 'desc');
		$this->db->limit($limit, $offset);
		$query = $this->db->get('project_request');
	
		$rows = $query->result_array();
	
		return $rows;
	}
	
	public function project_request_total_rows() {
	
		$total_rows = $this->db->count_all('project_request');
	
		return $total_rows;
	}
	
	public function project_request_delete_action($id) {
	
		$this->db->where('id', $id);
	
		$this->db->delete('project_request');
	}
	
	public function project_request_view($id) {
	
		$this->db->where('id', $id);
	
		$query = $this->db->get('project_request');
	
		$row = $query->row_array();
	
		return $row;
	}
	
	public function project_request_edit_action($id, $company_name, $user_name, $email_address, $phone_number, $kind, $budget, $schedule, $comment, $image) {
	
		$kind = json_encode($kind);
		
		$fields = array(
				'company_name' => $company_name,
				'user_name' => $user_name,
				'email_address' => $email_address,
				'phone_number' => $phone_number,
				'kind' => $kind,
				'budget' => $budget,
				'schedule' => $schedule,
				'comment' => $comment
		);
		
		if(!empty($image)) {
			$image = json_encode($image);
			$fields['image'] = $image;
		}
		
		$this->db->set($fields);
	
		$this->db->where('id', $id);
		
		$this->db->update('project_request');
	}
}

/* End of file admin_model.php */
/* Location: ./application/models/admin_model.php */